﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ZhCun.WebCommon.ApiModels
{
    public class ApiResult
    {
        /// <summary>
        /// 结果代码，0为成功
        /// </summary>
        public int code { set; get; }
        /// <summary>
        /// 用户提示消息
        /// </summary>
        public string msg { set; get; }
    }
    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// api返回的数据
        /// </summary>
        public T data { set; get; }
    }
    public class ApiResultArray<T> : ApiResult
    {
        /// <summary>
        /// api返回的数据
        /// </summary>
        public IEnumerable<T> data { set; get; }
        /// <summary>
        /// 如果分页list则返回总行数
        /// </summary>
        public int count { set; get; }
    }
    public class ApiResultFactory
    {
        public static ApiResult CreateResult(int code, string message)
        {
            return new ApiResult
            {
                code = code,
                msg = message
            };
        }
        public static ApiResult CreateResult(bool success, string message)
        {
            return new ApiResult
            {
                code = success ? 0 : 1,
                msg = message
            };
        }
        /// <summary>
        /// 创建一个执行成功的结果
        /// </summary>
        public static ApiResult CreateResult()
        {
            return CreateResult(true, "操作成功");
        }
        /// <summary>
        /// 创建一个成功并返回实体对象的结果
        /// </summary>
        public static ApiResult<T> CreateResult<T>(T data)
        {
            return new ApiResult<T>
            {
                code = 0,
                msg = "success",
                data = data
            };
        }
        /// <summary>
        /// 创建一个成功并返回集合对象的结果
        /// </summary>
        public static ApiResultArray<T> CreateArrayResult<T>(IEnumerable<T> data, int count = 0)
        {
            return new ApiResultArray<T>
            {
                code = 0,
                msg = "success",
                data = data,
                count = count
            };
        }
    }
}